package com.andy.qhb.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.entity.UsrMsgLog;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.utils.GmAction;
import com.andy.qhb.utils.GoConstant;
import com.andy.qhb.utils.SendUtil;
import com.andy.qhb.websocket.MySocketSession;

/**
 * 申请入群
 * @author cuiwm
 *
 */
@Component(GmAction.ACTION_PREFIX+GmAction.ACTION_117)
public class Action117 implements IWebAction{

	@Override
	public void doAction(MySocketSession myWebSocket, Map<String, Object> data) {
		GameUser gameUser = myWebSocket.getUser();
		
		Long groupId = MapUtils.getLong(data, "groupId");
		UsrGroups group = Global.getUsrGroup(groupId);
		
		if(group == null) {
			SendUtil.sendError(myWebSocket, GmAction.ACTION_117, "群不存在");
			return;
		}
		
		Long uid = gameUser.getId();
		List<UsrMsgLog> logs = group.rlogs();
		logs.forEach((log)->{
			if(log.getUid().equals(uid)) {
				return ;
			}
		});
		
		UsrMsgLog msgLog = new UsrMsgLog();
		msgLog.setType(GoConstant.MSG_1);
		msgLog.setUid(gameUser.getId());
		msgLog.setUidOther(groupId);
		DaoUtils.insert(msgLog);
		
		logs.add(msgLog);
		
		
	}



	
	
	
}
